<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserGetLogController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $userGameData = DB::table('user_get_log')
            ->select('user_get_log.*','users.username', 'users.unionid')
            ->leftJoin('users', 'user_get_log.user_id', '=', 'users.id')
            ->where(function ($query) {
                if (!empty(@$_GET['status']))
                    $query->orWhere('status', '=', @$_GET['status']);
                if (!empty($_GET['unionid']))
                    $query->orWhere('users.unionid', '=', $_GET['unionid']);
            })
            ->orderBy('id', 'desc')
            ->paginate(15)
            ->withPath('?status=' . @$_GET['status'] .'');

        return view('admin.user_get_log.index',['userGameData'=>$userGameData]);
    }

    // 充值
    public function recharge($id)
    {
        DB::table('user_get_log')->where('id', $id)->update(['status' => 2, 'recharge_time'=>time()]);
        return response()->json(['message' => '充值成功！', 'success' => 'true']);
    }

    // 刷号
    public function shuaHao($id)
    {
        $row  = DB::table('user_get_log')->where('id', $id)->first();
        DB::table('user_get_log')->where('id', $id)->update(['status' => 2, 'recharge_time'=>time()]);
        $user = DB::table('users')
            ->where('id', $row->user_id)
            ->first();
        $config = config('wechat');
        $app = Factory::officialAccount($config);
        $app->template_message->send([
            'touser' => $user->wx_unionid,
            'template_id' => 'JH7dKP1aBkf09VWtcW_pX_lusRKJ4Q9NiPRMTq3K0MQ',
            'url' => 'https://l3y.com/',
            'data' => [
                'first' => '乐享游福利赠送通知',
                'keyword1' => $row->game_num,
                'keyword2' => $row->game_name,
                'keyword3' =>  $row->server_name . ' '. $row->money,
                'keyword4' => '成功',
                'keyword5' => '请勿刷号！',
                'remark' => '祝您游戏愉快,如果有其他问题可咨询微信专属客服'
            ],
        ]);
        return response()->json(['message' => '反馈刷号成功！', 'success' => 'true']);
    }

    /**
     * 反馈
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMsg(Request $request)
    {
        $id = $request->id;
        $msg = $request->msg;
        if (!$id) {
            return response()->json(['message' => '请输入id', 'success' => 'false']);
        }
        if (!$msg) {
            return response()->json(['message' => '请输入反馈', 'success' => 'false']);
        }
        $log  = DB::table('user_get_log')->where('id', $id)->first();
        if (!$log) {
            return response()->json(['message' => '没有记录', 'success' => 'false']);
        }
        if ($log->status != 1) {
            return response()->json(['message' => '记录状态错误', 'success' => 'false']);
        }
        DB::table('user_get_log')->where('id', $id)->update(['status' => 3, 'msg'=>$msg]);
        return response()->json(['message' => '反馈成功！', 'success' => 'true']);
    }
}
